package com.gitee.cnlongdb.server.threads.query;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gitee.cnlongdb.basic.config.LongDbConfig;
import com.gitee.cnlongdb.basic.constant.LongDbConstant;
import com.gitee.cnlongdb.basic.entity.query.QueryResultEntity;
import com.gitee.cnlongdb.basic.entity.query.QueryThreadEntity;
import com.gitee.cnlongdb.server.connect.LongDbConnect;

/**
 * 系统名称：QueryHandlerThread.java
 * 模块名称：
 * 模块描述：多线程 - 查询器对象
 * 功能列表：
 * 模块作者：ZOUYONG
 * 开发时间：2018年12月5日 下午9:59:33
 * 模块路径：com.gitee.cnlongdb.server.threads.query.QueryHandlerThread
 * 更新记录：
 */
public class QueryHandlerThread extends Thread {

	private static final Logger logger = (Logger) LoggerFactory.getLogger(QueryHandlerThread.class);
	
	private QueryThreadEntity queryData;
	private CountDownLatch doneSignal;
	private LongDbConnect connect;
	private QueryResultEntity queryResult;
	
	public QueryHandlerThread(QueryThreadEntity queryData, CountDownLatch doneSignal, LongDbConnect connect, QueryResultEntity queryResult) {
		this.queryData = queryData;
		this.doneSignal = doneSignal;
		this.connect = connect;
		this.queryResult = queryResult;
	}
	
	@Override
	public void run() {
		// long time1 = System.currentTimeMillis();
		BufferedReader bufReader = null;
		BufferedInputStream bis = null;
		try {
			// FileReader:用来读取字符文件的便捷类。
			bis = new BufferedInputStream(new FileInputStream(queryData.getFile()));
			bufReader = new BufferedReader(
				new InputStreamReader(bis, LongDbConstant.longdbSystemCharset), 
				LongDbConfig.longdbTableFileBufferedReaderSize
			);
			String content = null;
			while ((content = bufReader.readLine()) != null) {
				if(!queryData.getQuery().isSortQuery() && !queryData.getQuery().isQueryCount() && 
						null != queryData.getTotalCount() && queryResult.getCurrQueryCount() >= queryData.getTotalCount()) {
					break;
				}
				if(connect.getQueryOperator().getWhereString(queryData.getQuery(), content)) {
					queryResult.addCurrQueryCount(1);
					if(!queryData.getQuery().isQueryCount()) {
						queryData.getObjectList().add(content);
					} else {
						content = null;
					}
				} else {
					content = null;
				}
			}
		}
		catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		finally {
			doneSignal.countDown();
			if (bufReader != null) {
				try {
					bufReader.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
					bufReader = null;
				}
			}
			if(null != bis) {
				try {
					bis.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
					bis = null;
				}
			}
		}
		// System.out.println("线程操作完成, 总耗时：" + Float.toString((System.currentTimeMillis() - time1) / 1000F) + " 秒.");
	}
	
}
